﻿using System;
using System.Text;
using IAM.Interface.Model.Interface;
using System.Reflection;
using System.Xml;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Model;

namespace IAM.Interface.Common
{
    /// <summary>
    /// xml和对象相互转换
    /// </summary>
    public class XmlObjConversion
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">转换的类型</param>
        /// <param name="xmlint">类实体转换为xml的接口</param>
        public XmlObjConversion(Type type, IConvertXml xmlint)
        {
            _type = type;
            _xmlint = xmlint;

            string[] assemblynames = type.Assembly.FullName.Split(new char[]{','});

            _acojb = (AbstractConvertObject)Assembly.Load(assemblynames[0]).CreateInstance(_type.FullName);

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actiontype">操作类型</param>
        /// <param name="xmldocElementName">包头节点名称</param>
        public XmlObjConversion(ActionType actiontype,string xmldocElementName)
        {
            if (xmldocElementName.ToLower() == "requestdata")//请求信息包
            {
                #region ActionType
                switch (actiontype)
                {
                    case ActionType.IDEN_MANAGE://身份信息管理
                        _acojb = new Identity();
                        break;
                    case ActionType.IDEN_GETACMODEL://获取权限模型
                        _acojb = new RequestData();
                        break;
                    case ActionType.IDEN_GETUSERACI://获取用户权限信息
                        _acojb = new AccountInfo();
                        break;
                    case ActionType.IDEN_SETUSERACI://设置用户权限信息
                        _acojb = new ACI();
                        break;
                    case ActionType.ORG_UPDATE://组织信息更新
                        _acojb = new OrganizeInfo();
                        break;
                    case ActionType.ORG_CREATE://组织机构创立
                        _acojb = new OrganizeInfo();
                        break;
                    case ActionType.ORG_DELETE://组织结构删除（包括合并）
                        _acojb = new OrganizeDeleteParameter();
                        break;
                    case ActionType.DM_GETMODULELIST://获取可代理模块列表
                        _acojb = new RequestData();
                        break;
                    case ActionType.DM_GETDELEINFO://获取用户代理信息
                        _acojb = new AccountInfo();
                        break;
                    case ActionType.DM_SETDELEINFO://设置用户代理信息
                        _acojb = new AIDelegations();
                        break;
                    //case "IDEN_USERLIST"://获取用户信息列表
                    //    xn = (XmlNode)xe;
                    //    break;
                    //case "AUDIT_FETCH"://获取审计信息
                    //    xn = xe.SelectSingleNode("Logs");
                    //    break;
                }
                #endregion
            }
            else//应答信息包
            {
                #region ActionType
                switch (actiontype)
                {
                    case ActionType.IDEN_MANAGE://身份信息管理
                        _acojb = new AccountInfo();
                        break;
                    case ActionType.IDEN_GETACMODEL://获取权限模型
                        _acojb = new ACMMode();
                        break;
                    case ActionType.IDEN_GETUSERACI://获取用户权限信息
                        _acojb = new ACI();
                        break;
                    case ActionType.IDEN_SETUSERACI://设置用户权限信息
                        _acojb = new ACI();
                        break;
                    case ActionType.ORG_UPDATE://组织信息更新
                        _acojb = new ResponseData();
                        break;
                    case ActionType.ORG_CREATE://组织机构创立
                        _acojb = new ResponseData();
                        break;
                    case ActionType.ORG_DELETE://组织结构删除（包括合并）
                        _acojb = new ResponseData();
                        break;
                    case ActionType.DM_GETMODULELIST://获取可代理模块列表
                        _acojb = new Modules();
                        break;
                    case ActionType.DM_GETDELEINFO://获取用户代理信息
                        _acojb = new AIDelegations();
                        break;
                    case ActionType.DM_SETDELEINFO://设置用户代理信息
                        _acojb = new AIDelegations();
                        break;
                    //case "IDEN_USERLIST"://获取用户信息列表
                    //    xn = (XmlNode)xe;
                    //    break;
                    //case "AUDIT_FETCH"://获取审计信息
                    //    xn = xe.SelectSingleNode("Logs");
                    //    break;
                }
                #endregion
            }


        }
        /// <summary>
        /// 实体类转换xml接口
        /// </summary>
        /// <param name="xmlint"></param>
        public XmlObjConversion(IConvertXml xmlint)
        {
            _xmlint = xmlint;
        }
        #endregion
        #region 属性
        private Type _type = null;
        private IConvertXml _xmlint = null;
        private AbstractConvertObject _acojb = null;
        private XmlNode _xmlnode = null;
        /// <summary>
        /// 转换的类型
        /// </summary>
        public Type Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }
        /// <summary>
        /// 类实体转换为xml的接口
        /// </summary>
        public IConvertXml IConvertXml
        {
            get
            {
                return _xmlint;
            }
            set
            {
                _xmlint = value;
            }
        }
        /// <summary>
        /// xml转换为obj的接口
        /// </summary>
        public AbstractConvertObject AbConvertObj
        {
            set
            {
                _acojb = value;
            }
            get
            {
                return _acojb;
            }
        }
        /// <summary>
        /// 当前节点
        /// </summary>
        public XmlNode CurrentXmlNode
        {
            get
            {
                return _xmlnode;
            }
            set
            {
                _xmlnode = value;
            }
        }
        #endregion
        #region 方法
        /// <summary>
        /// 将实体系列化成为XML
        /// </summary>
        /// <returns></returns>
        public string GetXml()
        {
            return IConvertXml.XML;
        }
        /// <summary>
        /// Xml反序列化为对象
        /// </summary>
        /// <returns></returns>
        public object ConvertToObj()
        {
            AbConvertObj.CurrentXmlNode = this.CurrentXmlNode;
            return AbConvertObj.ConvertToObject();
        }
        #endregion
    }
}
